package com.zqcy.workbench.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bambooclod.epassself.api.EpassSelfSDK;
import com.bambooclod.epassself.callback.CommonResultListener;
import com.bambooclod.epassself.callback.GetUserInfoListener;
import com.shinemo.base.core.utils.Jsons;
import com.shinemo.base.core.utils.ToastUtil;
import com.shinemo.minisdk.config.MiniSdk;
import com.zqcy.workbench.activity.LocationSearchActivity;
import com.zqcy.workbench.R;
import com.zqcy.workbench.activity.LoginActivity;
import com.zqcy.workbench.bean.UserInfo;
import com.zqcy.workbench.utils.Utils;
import com.zqcy.workbench.utils.Constants;
import com.onemdos.storage.kv.MDOSPreferencesManager;
import com.onemdos.upgrade.MDOSUpgrade;
import com.onemdos.upgrade.MDOSUpgradeCallback;
import com.onemdos.upgrade.dialog.MDOSUpdateDialog;
import com.onemdos.upgrade.model.MDOSUpgradeInfo;

public class MyFragment extends Fragment {

    public MyFragment() {
        // Required empty public constructor
    }

    public static MyFragment newInstance() {
        MyFragment fragment = new MyFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        Log.e("tag", "@@@@ getUserInfo 调用");
        EpassSelfSDK.Builder().getUserInfo(getContext(), 1, new GetUserInfoListener() {
            @Override
            public void success(String response) {
                Log.e("tag", "@@@@ success:" + response);
                UserInfo userInfo = Jsons.fromJson(response, UserInfo.class);
                UserInfo.Data userData = userInfo.getData();
                TextView tvHeadName = view.findViewById(R.id.tv_head_name);
                tvHeadName.setText(Utils.getSimpleName(userData.getName()));

                TextView tvName = view.findViewById(R.id.tv_name);
                tvName.setText(userData.getName());

                TextView tvPhone = view.findViewById(R.id.tv_phone);
                tvPhone.setText(userData.getPhone());
            }

            @Override
            public void error(String code, String message) {
                Log.e("tag", "@@@@ error code:" + code + " message:" + message);
                ToastUtil.show(getContext(), "登录过期，请重新登录");
                logout();
            }
        });


        view.findViewById(R.id.ll_logout).setOnClickListener(view1 -> {
            ProgressDialog pd = ProgressDialog.show(getContext(), "", "正在退出登录", true);
            EpassSelfSDK.Builder().logout(getContext(), new CommonResultListener() {
                @Override
                public void success() {
                    pd.dismiss();
                    ToastUtil.show(getContext(), "注销成功");
                    logout();
                }

                @Override
                public void error(String code, String message) {
                    pd.dismiss();
                    ToastUtil.show(getContext(), message);
                    logout();
                }
            });
        });
        view.findViewById(R.id.my_checkVersion).setOnClickListener(view1 -> {
            MDOSUpgrade.checkNewVersion(new MDOSUpgradeCallback() {
                @Override
                public void getUpgradeInfo(MDOSUpgradeInfo mdosUpgradeInfo) {
                    if (getActivity() != null && !getActivity().isFinishing() && mdosUpgradeInfo != null) {
                        MDOSUpdateDialog updateDialog = new MDOSUpdateDialog(getActivity(), mdosUpgradeInfo);
                        updateDialog.show();
                    }
                }

                @Override
                public void noUpgradeInfo(int errorCode) {
                    ToastUtil.show(getActivity(), "暂无新版本,无需更新");
                }
            });
        });
        view.findViewById(R.id.my_location_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LocationSearchActivity.startActivity(getActivity());
            }
        });
        return view;
    }

    private void logout() {
        MiniSdk.killLivedMiniPrograms(true);
        MDOSPreferencesManager.getInstance(getContext(), Constants.APP_SP_NAME).putBoolean(Constants.IS_LOGIN, false).apply();
        LoginActivity.startActivity(getContext());
        if (getActivity() != null && !getActivity().isDestroyed()) {
            getActivity().finish();
        }
    }
}